package com.ssm.reader.controller.management;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ssm.reader.entity.Book;
import com.ssm.reader.entity.Evaluation;
import com.ssm.reader.mapper.EvaluationMapper;
import com.ssm.reader.service.EvaluationService;
import com.ssm.reader.service.exception.BussinessException;
import org.junit.Test;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.sql.BatchUpdateException;
import java.util.HashMap;
import java.util.Map;

/*
 *
 *
 *@data 2022-05-04
 */
@Controller
@RequestMapping("/management/evaluation")
public class MEvaluationController {
    @Resource
    private EvaluationService evaluationService;

    //这个页面在未登录时，输入相应的地址也会出现。不安全，需要加入登录验证功能
    @GetMapping("/index.html")
    public ModelAndView showBook(){
        return new ModelAndView("/management/evaluation");
    }

    @GetMapping("/list")
    @ResponseBody
    public Map list(Integer page,Integer limit){
        if (page == null){
            page = 1;
        }

        if (limit == null){
            limit = 20;
        }
        PageHelper.startPage(page,limit);
        Page<Evaluation> pages = (Page)evaluationService.selectPages();
        Map result = new HashMap();
        result.put("code",0);
        result.put("msg","success");
        result.put("data",pages.getResult());//当前页面数据
        result.put("count",pages.getTotal());//未分页时记录总数
        return  result;
    }

    @PostMapping("/disable")
    @ResponseBody
    public Map disable(Long evaluationId,String reason){
        Map result = new HashMap();
        try{
            evaluationService.disableById(evaluationId,reason);
            result.put("code",0);
            result.put("msg","success");
        }catch (BussinessException ex){
            ex.printStackTrace();
            result.put("code",ex.getCode());
            result.put("msg",ex.getMsg());
        }

        return result;
    }

}
